package com.geek.alert.domain;

import com.geek.alert.beans.AlertRule;
import com.geek.alert.beans.ApiStatInfo;
import com.geek.alert.beans.Notification;
import com.geek.alert.handler.AlertHandler;
import lombok.Builder;

import java.util.ArrayList;
import java.util.List;

public class AlertProcessor {
    private AlertRule rule;
    private Notification notification;
    private List<AlertHandler> handlers = new ArrayList<>();

    @Builder
    public AlertProcessor(AlertRule rule, Notification notification) {
        this.rule = rule;
        this.notification = notification;
    }

    public void process(ApiStatInfo apiStatInfo) {
        for (AlertHandler handler : handlers) {
            handler.check(apiStatInfo, rule, notification);
        }
    }

    public AlertProcessor next(AlertHandler handler) {
        handlers.add(handler);
        return this;
    }


    // 饿汉式单例
    // 这里注释掉是因为使用单例的方式创建的话，报警规则动态注入不好实现，最终想实现的效果是，增加规则，这个处理类不需要改动
    /*private static final ApplicationContext instance = new ApplicationContext();

    private ApplicationContext() {
        initializeBeans();
    }

    public static ApplicationContext getInstance() {
        return instance;
    }*/
}